
<?php
$username = $_POST["username"];
$password  = $_POST["password"];
// echo $username.'<br>';
// echo $password.'<br>';
$host = 'localhost';  // MySQL主机
$dbname = 'test_db';  // 数据库名
$sql_user = 'hwq';  // 用户名
$sql_passwd = 'hwq1123';  // 密码
try {
    $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8";  // 数据源名称
    $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  // 设置错误处理模式为异常模式
	        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,  // 设置默认的结果集获取模式为关联数组模式
		    ];
    $pdo = new PDO($dsn, $sql_user, $sql_passwd, $options);  // 创建PDO对象
    // echo "pdo object created;".'<br>';
    // echo 'select password from accounts where username =\''. $username.'\''.'<br>';
    $get_record = $pdo->prepare('select password, link from accounts where username =\''. $username.'\'');
    $get_record->execute();
    $record = $get_record->fetchAll();
    // echo "record:".'<br>';
    // print_r($record);
    // echo "record[0][password]:".'<br>';
    // echo $record[0]['password'].'<br>';

    if ($record[0]['password'] == $password)
    {// echo "密码正确";
    header("Location:./".$record[0]['link']);}
    else{echo "密码错误";}
    
} catch (PDOException $e) {
    
    echo "数据库错误: " . $e->getMessage();

}

?>